package com.jadeen.common.controller;

import com.jadeen.common.constant.CacheConstants;
import com.jadeen.common.domain.AjaxResult;
import com.jadeen.common.domain.dto.EmailDTO;
import com.jadeen.common.utils.EmailUtil;
import com.jadeen.common.utils.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * @author: Jadeen
 * @date: 2025/9/12 17:32
 */
@RestController
@RequestMapping("/email")
public class EmailController {
    @Autowired
    private EmailUtil emailUtil;
    @Autowired
    private RedisUtil redisUtil;


    @GetMapping("/sentCode")
    public AjaxResult sentEmail(String email){
        redisUtil.set(CacheConstants.CAPTCHA_CODE_KEY + email, "123456");
        Map<String, Object> map = new HashMap<>();
        map.put("content", "123456");
        EmailDTO emailDTO = EmailDTO.builder()
                .template("common.html")
                .email(email)
                .subject("验证码")
                .commentMap(map).build();

        try {
            emailUtil.sendHtmlMail(emailDTO);
            return AjaxResult.success("验证码发送成功");
        } catch (Exception e) {
            return AjaxResult.error("验证码发送失败");
        }
    }

}
